//
//  DUCalTextButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/12/15.
//

import Cocoa

@IBDesignable
class DUCalTextButton: BaseViewButton {

    private var text:DULabel!
    private var back1:CALayer!
    private var back2:CALayer!
    var labelId: String?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize() {
        let w = self.frame.width
        let h = self.frame.height
        let f = NSMakeRect(0, 0, w, h)
        
        text = DULabel(frame: NSMakeRect(0, 0, w, h))
        text.frame = f
        text.cell = VerticallyCenteredTextFieldCell()
        text.alignment = .center
        text.textColor = NSColor.darkGray
        text.font = NSFont.systemFont(ofSize: h * 0.4)
        back1 = CALayer()
        back1.frame = f
        back2 = CALayer()
        back2.isHidden = true
        back2.frame = f
    }
    
    override func layout() {
        if let n = self.layer {
            n.cornerRadius = 6
            if self.layer!.sublayers == nil {
                n.addSublayer(back1)
                n.addSublayer(back2)
                self.addSubview(text)
            }
        }
    }
    
    override func mouseInEnterd(sender: BaseViewButton, event: NSEvent) {
        back2.isHidden = false
    }
    
    override func mouseInExited(sender: BaseViewButton, event: NSEvent) {
        back2.isHidden = true
    }
    
    @IBInspectable var buttonText: String = "" {
        didSet {
            text.stringValue = buttonText
        }
    }
    
    @IBInspectable var styleNumber: Int = -1 {
        didSet {
            if let n = DefaultButtonStyleEnum.init(rawValue: styleNumber) {
                settingStyle = n
            }
        }
    }
    
    struct TextButtonStyle {
        /**
         默认背景颜色，默认没有颜色
         */
        var back1Color:CGColor?
        /**
         鼠标移入后的背景颜色，可设置透明度以达到和默认背景重叠，默认没有颜色
         */
        var back2Color:CGColor?
        /**
         按钮字体颜色，默认为白色
         */
        var textColor:NSColor = NSColor.white
        /**
         圆角弧度，默认为6
         */
        var borderRadius: CGFloat = 6
    }
    
    public func setStyle(style: TextButtonStyle) {
        back1.backgroundColor = style.back1Color
        back2.backgroundColor = style.back2Color
        self.layer?.cornerRadius = style.borderRadius
        text.textColor = style.textColor
    }
    
    var settingStyle: DefaultButtonStyleEnum? {
        didSet {
            if settingStyle == nil {
                return
            }
            if settingStyle == .confirm {
                setStyle(style: DefaultButtonStyle.confirmStyle)
            } else if settingStyle == .cancel {
                setStyle(style: DefaultButtonStyle.cancelStyle)
            }
        }
    }
    
    enum DefaultButtonStyleEnum: Int {
        case confirm = 0
        case cancel = 1
    }
    
    fileprivate class DefaultButtonStyle {
        
        static var confirmStyle: TextButtonStyle {
            get {
                var t = TextButtonStyle()
                t.back1Color = NSColor.blue.cgColor
                t.back2Color = CGColor(red: 1, green: 1, blue: 1, alpha: 0.3)
                t.textColor = NSColor.white
                t.borderRadius = 6
                return t
            }
        }
        
        static var cancelStyle: TextButtonStyle {
            get {
                var t = TextButtonStyle()
                t.back1Color = NSColor.gray.cgColor
                t.back2Color = CGColor(red: 1, green: 1, blue: 1, alpha: 0.3)
                t.textColor = NSColor.white
                t.borderRadius = 6
                return t
            }
        }
        
    }
    
    
}


